<hr>
<p>title: 349.intersection-of-two-arrays &amp; 350.intersection-of-two-arrays-ii<br>date: 2021-11-18 14:39:54<br>mathjax: true<br>tags:</p>
<pre><code>- LeeCode
</code></pre><p>categories: </p>
<pre><code>- LeeCode
</code></pre><p>hidden: true</p>
<h2 id="cateHidden-false"><a href="#cateHidden-false" class="headerlink" title="cateHidden: false"></a>cateHidden: false</h2><h3 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h3><h4 id="I"><a href="#I" class="headerlink" title="I"></a>I</h4><blockquote>
<p>Given two integer arrays nums1 and nums2, return an array of their intersection.</p>
<p>Each element in the result must be unique and you may return the result in any order.</p>
</blockquote>
<h4 id="II"><a href="#II" class="headerlink" title="II"></a>II</h4><blockquote>
<p>Given two integer arrays nums1 and nums2, return an array of their</p>
<p>intersection. Each element in the result must appear as many times as it shows</p>
<p>in both arrays and you may return the result in any order.</p>
</blockquote>
<h3 id="测试用例"><a href="#测试用例" class="headerlink" title="测试用例"></a>测试用例</h3><pre><code class="lang-bash"># I
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Explanation: [4,9] is also accepted.

# II
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
Explanation: [9,4] is also accepted.
</code></pre>
<h3 id="题解"><a href="#题解" class="headerlink" title="题解"></a>题解</h3><h4 id="I-1"><a href="#I-1" class="headerlink" title="I"></a>I</h4><p>暴力循环。</p>
<pre><code class="lang-js">var intersection = function(nums1, nums2) {
    let intersection_arr = new Set(), result = [];
    let set1 = new Set(nums1), set2 = new Set(nums2);
    set1.forEach(n1 =&gt; {
        set2.forEach(n2 =&gt; {
            if(n1 === n2) {
                intersection_arr.add(n1);
            }
        })
    })
    intersection_arr.forEach(n =&gt; {
        result.push(n)
    })
    return result;
};
</code></pre>
<h4 id="II-1"><a href="#II-1" class="headerlink" title="II"></a>II</h4><p>同样的暴力循环。</p>
<pre><code class="lang-js">var intersect = function(nums1, nums2) {
    let result = [];
    for(let i in nums1) {
        for(let j in nums2) {
            if(nums1[i] === nums2[j]) {
                result.push(nums1[i]);
                nums2[j] = null; // 去除已被记录的数字
                break;
            }
        }
    }
    return result;
};
</code></pre>
<h3 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h3><h4 id="I-2"><a href="#I-2" class="headerlink" title="I"></a>I</h4><blockquote>
<p>Accepted</p>
<p>55/55 cases passed (100 ms)</p>
<p>Your runtime beats 27.44 % of javascript submissions</p>
<p>Your memory usage beats 42.4 % of javascript submissions (40.4 MB)</p>
</blockquote>
<h4 id="II-2"><a href="#II-2" class="headerlink" title="II"></a>II</h4><blockquote>
<p>Accepted</p>
<p>55/55 cases passed (168 ms)</p>
<p>Your runtime beats 5.09 % of javascript submissions</p>
<p>Your memory usage beats 5.03 % of javascript submissions (45.1 MB)</p>
</blockquote>
